package com.fgs.common.widget.itemView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fgs.common.R;
import com.fgs.common.utils.CommonUtil;


/**
 * @author fgs
 * @date 2020/3/29
 * @description
 **/
public class DetailItemView extends LinearLayout {
    private LinearLayout mLinearLayout;
    private TextView mTipTextView;
    private TextView mTitleTextView;
    private TextView mContentTextView;
    private ImageView mIconImageView;
    private String mTitle;
    private String mContent;
    private String mContentHint;
    private int mTitleTextColor;
    private int mContentTextColor;
    private int mContextHintColor;
    private boolean isShowTipText;


    public DetailItemView(Context context) {
        super(context);
        initView(context, null);
    }

    public DetailItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DetailItemView);
        int icon;
        int paddingTop;
        int paddingLeft;
        int paddingRight;
        int paddingBottom;
        int contentGravity;
        try {
            mTitle = typedArray.getString(R.styleable.DetailItemView_DetailItemTitle);
            mContent = typedArray.getString(R.styleable.DetailItemView_DetailItemContent);
            mContentHint = typedArray.getString(R.styleable.DetailItemView_DetailItemContentHint);
            mTitleTextColor = typedArray.getColor(R.styleable.DetailItemView_DetailItemTitleTextColor, context.getResources().getColor(R.color.colorTextBlack));
            mContentTextColor = typedArray.getColor(R.styleable.DetailItemView_DetailItemContentTextColor, context.getResources().getColor(R.color.colorTextPrimary));
            mContextHintColor = typedArray.getColor(R.styleable.DetailItemView_DetailItemContentHintTextColor, Color.GRAY);
            icon = typedArray.getColor(R.styleable.DetailItemView_DetailItemIcon, 0);
            paddingTop = typedArray.getDimensionPixelSize(R.styleable.DetailItemView_DetailItemPaddingTop, (int) CommonUtil.convertDpToPixel(5));
            paddingLeft = typedArray.getDimensionPixelSize(R.styleable.DetailItemView_DetailItemPaddingLeft, (int) CommonUtil.convertDpToPixel(0));
            paddingRight = typedArray.getDimensionPixelSize(R.styleable.DetailItemView_DetailItemPaddingRight, (int) CommonUtil.convertDpToPixel(10));
            paddingBottom = typedArray.getDimensionPixelSize(R.styleable.DetailItemView_DetailItemPaddingBottom, (int) CommonUtil.convertDpToPixel(5));
            contentGravity = typedArray.getInt(R.styleable.DetailItemView_DetailItemContentGravity, 0);
            isShowTipText = typedArray.getBoolean(R.styleable.DetailItemView_DetailItemIsTipShow, false);
        } finally {

            typedArray.recycle();
        }


        View view = LayoutInflater.from(context).inflate(R.layout.layout_detail_item, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        addView(view);

        mLinearLayout = view.findViewById(R.id.layout_detail_item_linearLayout);
        mTipTextView = view.findViewById(R.id.layout_detail_item_tipTextView);
        mTitleTextView = view.findViewById(R.id.layout_detail_item_titleTextView);
        mContentTextView = view.findViewById(R.id.layout_detail_item_contentTextView);
        mIconImageView = view.findViewById(R.id.layout_detail_item_imageView);

        setTitle(mTitle);
        setContent(mContent);
        setContentHint(mContentHint);
        setTitleTextColor(mTitleTextColor);
        setContentTextColor(mContentTextColor);
        setContextHintColor(mContextHintColor);
        setShowTip(isShowTipText);
        setIcon(icon);
        setContentGravity(contentGravity);
        setLinearLayoutPadding(paddingTop, paddingLeft, paddingRight, paddingBottom);
    }

    private void setContentGravity(int gravity) {
        switch (gravity) {
            case 0:
                mContentTextView.setGravity(Gravity.LEFT);
                break;
            case 1:
                mContentTextView.setGravity(Gravity.RIGHT);
                break;
            case 2:
                mContentTextView.setGravity(Gravity.CENTER);
                break;
        }
    }

    private void setLinearLayoutPadding(int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) {
        mLinearLayout.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
    }

    public void setIcon(int icon) {
        if (icon > 0) {
            mIconImageView.setVisibility(View.VISIBLE);
            mIconImageView.setBackgroundResource(icon);
        } else {
            mIconImageView.setVisibility(View.GONE);
            mIconImageView.setBackgroundResource(0);
        }
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        this.mTitle = title;
        mTitleTextView.setText(title);
    }

    public String getContent() {

        return mContentTextView.getText().toString().trim();
    }

    public void setContent(String content) {
        this.mContent = content;
        mContentTextView.setText(content);
    }

    public String getContentHint() {
        return mContentHint;
    }

    public void setContentHint(String contentHint) {
        this.mContentHint = contentHint;
        mContentTextView.setHint(contentHint);
    }

    public int getTitleTextColor() {
        return mTitleTextColor;
    }

    public void setTitleTextColor(int titleTextColor) {
        this.mTitleTextColor = titleTextColor;
        mTitleTextView.setTextColor(titleTextColor);
    }

    public int getContentTextColor() {
        return mContentTextColor;
    }

    public void setContentTextColor(int contentTextColor) {
        this.mContentTextColor = contentTextColor;
        mContentTextView.setTextColor(contentTextColor);
    }

    public int getContextHintColor() {
        return mContextHintColor;
    }

    public void setContextHintColor(int contextHintColor) {
        this.mContextHintColor = contextHintColor;
        mContentTextView.setHintTextColor(contextHintColor);
    }


    private void setShowTip(boolean isShowTipText) {

        this.isShowTipText = isShowTipText;
        if (isShowTipText) {
            mTipTextView.setVisibility(View.VISIBLE);
        } else {
            mTipTextView.setVisibility(View.INVISIBLE);
        }
    }

}
